/*
    方法1：动态规划
    遍历更新最大和的序列
 */
var maxSubArray = function (nums) {
    let pre = 0, maxSum = nums[0];
    nums.forEach((x) => {
       pre = Math.max(pre + x, x);
       maxSum = Math.max(maxSum, pre);
    });
    return maxSum;
}
console.log(maxSubArray([-2,1,-3,4,-1,2,1,-5,4]));

/*
    方法2：分治法
 */